package com.myn.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.myh.springcloud.pojo.User;

@RestController
public class UserConsumerController {

	// 理解：消费者不应该有service
	// RestTemplate 供我们使用直接调用 注册到spring
	//提供多种便捷访问远程http服务的方法 简单的Restful的服务模板
//	private static final String REST_USER_PREFIX="http://127.0.0.1:8001";
	
	//Ribbon 我们这里的地址应该是一个变量 通过服务名来拿 使用之后只能根据服务名那拿
//	private static final String REST_USER_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";
	private static final String REST_USER_PREFIX="http://SPRINGCLOUD-CONFIG-DEV";
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("/consumer/user/get/{id}")
	public User getUserById(@PathVariable("id")Long id) {
		return restTemplate.getForObject(REST_USER_PREFIX+"/user/get/"+id,User.class);
	}
	
	@GetMapping("/consumer/user/getAll")
	public List<User> getAll() {
		return restTemplate.getForObject(REST_USER_PREFIX+"/user/getAll",List.class);
	}
	
	
	@RequestMapping("/consumer/user/add")
	public boolean addUser( User user) {
		System.out.println(user);
		return restTemplate.getForObject(REST_USER_PREFIX+"/user/add",Boolean.class);
	}
	
	
}
